@extends('home.parent')
@section('title','消息管理')
@section('head_js')
@endsection
@section('head_css')
<link rel="stylesheet" href="{{ asset('/css/dataTables.bootstrap.css') }}">
<link rel="stylesheet" href="{{ asset('/css/toggleButtonsMain.css') }}">
<link rel="stylesheet" href="{{ asset('/css/datepicker3.css') }}">
@endsection
@section('content')
  .mytc{
  width: 240px;
  max-height: 200px;
  overflow: auto;

  }
  <div class="content-wrapper">
    <section class="content-header">
      <h1>
        消息管理
        <small></small>
      </h1>
      <ol class="breadcrumb">
        <li><a href="{{URL('admin')}}"><i class="fa fa-dashboard"></i> 首页</a></li>
        <li class="active">消息管理</li>
      </ol>
    </section>
    <section class="content">
      <div class="row">
        <div class="col-xs-12">
          <div class="box">
            <div class="box-header">
              <br />
              <div class="form-inline pull-left">
                <div class="input-group input-group-sm" style="margin-left: 20px;">
                  <div class="input-group-btn" >
                    <a href="{{ URL('admin/message/create') }}" class="btn btn-info">发布消息</a>
                  </div>
                </div>
              </div>
              <div class="form-inline pull-right">
                <fieldset>
                  <div class="input-group input-group-sm" >
                    <span class="input-group-addon"><strong>消息标题</strong></span>
                    <input type="text" class="form-control" id="search_title">
                  </div>
                  <div class="input-group input-group-sm" >
                    <span class="input-group-addon"><strong>开始日期</strong></span>
                    <input type="text" class="form-control pull-right" id="search_start_time" style="width:200px;" placeholder="开始日期" autocomplete="off">
                  </div>
                  <div class="input-group input-group-sm">
                    <span class="input-group-addon"><strong>截止日期</strong></span>
                    <input type="text" class="form-control pull-right" id="search_end_time" style="width:200px;" placeholder="截止日期" autocomplete="off">
                  </div>
                  <div class="input-group input-group-sm">
                    <div class="input-group-btn" >
                      <button type="button"  class="btn btn-primary" onclick="searchData()"><i class="fa fa-search"></i></button>
                    </div>
                  </div>
                </fieldset>
              </div>
            </div>
            <!-- /.box-header -->
            <div class="box-body">
              <table id="data_list" class="table table-bordered table-striped">
                <thead>
                  <tr>
                    <th>编号</th>
                    <th>标题</th>
                    <th>内容</th>
                    <th>发送人</th>
                    <th>创建人</th>
                    <th>发送用户</th>
                    <th>发送时间</th>
                    <th>状态</th>
                    <th>操作</th>
                  </tr>
                </thead>
                <tbody>
                </tbody>
                <tfoot>
                  <tr>
                    <th>编号</th>
                    <th>标题</th>
                    <th>内容</th>
                    <th>发送人</th>
                    <th>创建人</th>
                    <th>发送用户</th>
                    <th>发送时间</th>
                    <th>状态</th>
                    <th>操作</th>
                  </tr>
                </tfoot>
              </table>
            </div>
          </div>
        </div>
      </div>
    </section>
  </div>
  <div class="modal fade" id="viewDetails">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span></button>
          <h4 class="modal-title">某某用户详情</h4>
        </div>
        <div class="modal-body" style="max-height:300px;overflow:auto;padding:10px 100px;"></div>
      </div>
    </div>
  </div>
@endsection
@section('foot_js')
<script src="{{ asset('/js/jquery.dataTables.min.js') }}"></script>
<script src="{{ asset('/js/dataTables.bootstrap.min.js') }}"></script>
<script src="{{ asset('/js/jquery.slimscroll.min.js') }}"></script>
<script src="{{ asset('/js/fastclick.js') }}"></script>
<script src="{{ asset('/js/tools.js') }}"></script>
<script src="{{ asset('/js/apk.js') }}"></script>
<script src="{{ asset('/js/bootstrap-datepicker.js') }}"></script>
<script>
var list_ajax;
$(function () {
  $("#search_start_time").datepicker({
    language: "zh-CN",
    autoclose: true,//选中之后自动隐藏日期选择框
    format: "yyyy-mm-dd"//日期格式，详见 http://bootstrap-datepicker.readthedocs.org/en/release/options.html#format
  });
  $("#search_end_time").datepicker({
    language: "zh-CN",
    autoclose: true,//选中之后自动隐藏日期选择框
    format: "yyyy-mm-dd"//日期格式，详见 http://bootstrap-datepicker.readthedocs.org/en/release/options.html#format
  });
//提示信息
  var lang = puppet.langShow();
  //初始化表格
  list_ajax = $("#data_list").dataTable({
    language: lang, //提示信息
    autoWidth: false, //禁用自动调整列宽
    stripeClasses: ["odd", "even"], //为奇偶行加上样式，兼容不支持CSS伪类的场合
    processing: true, //隐藏加载提示,自行处理;
    serverSide: true, //启用服务器端分页
    searching: false, //禁用原生搜索
    //"sSearch": "搜索:",
    orderMulti: false, //启用多列排序
    order: [[0, 'desc']], //取消默认排序查询,否则复选框一列会出现小箭头
    renderer: "bootstrap", //渲染样式：Bootstrap和jquery-ui
    pagingType: "simple_numbers", //分页样式：simple,simple_numbers,full,full_numbers
    columnDefs: [
      {
        "targets": [1,2,3,4,5,6,7,8], //列的样式名
        "orderable": false    //包含上样式名‘nosort’的禁止排序
      },
      {
        targets: 6,
        render: function (data, type, row, meta) {
          switch(data) {
            case 1:
              return '<div class="checkbox checkbox-slider--b-flat"><label>'+
                '<input type="checkbox" checked="" onclick="actionData(' + row.id + ',0)"><span></span></label></div>';
              break;
            case 0:
              return '<div class="checkbox checkbox-slider--b-flat"><label>'+
                '<input type="default" checked="" onclick="actionData(' + row.id + ',1)"><span></span></label></div>';
              break;
            default:
              return '';
              break;
          }
        }
      },
      {
        targets: 8,
        render: function (data, type, row, meta) {
            return  "<a href='{{ URL('admin/message/view') }}/"+row.id+"' title='查看' aria-label='View' data-pjax='0'><span class='glyphicon glyphicon-eye-open'></span></a>&nbsp;&nbsp;&nbsp;&nbsp;"+
                    "<a href='{{ URL('admin/message/edit') }}/"+row.id+"' title='修改' aria-label='Update' data-pjax='0'><span class='glyphicon glyphicon-pencil'></span></a>&nbsp;&nbsp;&nbsp;&nbsp;"+
                    "<a href='javascript:void(0);' title='删除' aria-label='Delete' data-pjax='0' onclick='actionData("+row.id+",-1)'><span class='glyphicon glyphicon-trash'></span></a>";
        }
      }
    ],
    ajax: function (data, callback, settings) {
      //封装请求参数
      var param = {};
      param.limit = data.length; //页面显示记录条数，在页面显示每页显示多少项的时候
      param.start = data.start; //开始的记录序号
      param.page = (data.start / data.length) + 1; //当前页码
      param.order = data.order;
      param.title = $('#search_title').val();
      param.start_date = $('#search_start_time').val();
      param.end_date = $('#search_end_time').val();
      $.ajax({
        type: "POST",
        url: "{{ URL('admin/message/list/ajax') }}",
        cache: false, //禁用缓存
        data: param, //传入组装的参数
        dataType: "json",
        success: function (result) {
          var returnData = {};
          returnData.draw = data.draw; //这里直接自行返回了draw计数器,应该由后台返回
          returnData.recordsTotal = result.total; //返回数据全部记录
          returnData.recordsFiltered = result.total; //后台不实现过滤功能，每次查询均视作全部结果
          returnData.data = result.data; //返回的数据管理
          callback(returnData);
        }
      });
    },
    //管理表头字段
    columns: [
      {"data":"id"},
      {"data":"title"},
      {"data":"content"},
      {"data":"send_name"},
      {"data":"admin_name"},
      {"data":"type"},
      {"data":"create_time"},
      {"data":"status"},
      {"data":"cz"}
    ],
    "fnDrawCallback":function(){
      $("#data_list_paginate").children('ul').append("<li style='margin-left:5px;'>&nbsp;&nbsp;<label style='margin-top:-5px;'>到第</lable>&nbsp;<input style='width:40px;hidden:none' class='margin text-center' id='changePage' type='text'>&nbsp;&nbsp;<label>页</lable>&nbsp;</li> <li style='margin-left:5px;float:right;' class='paginate_button'><a style='margin-bottom:5px' href='javascript:void(0);'id='dataTable-btn'>确认</a></li>");
      var oTable = $("#data_list").dataTable();
      $('#dataTable-btn').click(function(e){
        var redirectpage = 0;
        if($("#changePage").val() && $("#changePage").val() > 0) {
          redirectpage = $("#changePage").val() - 1;
        }
        oTable.fnPageChange(redirectpage);
      });
    },
  }).api();
});

function actionData(id,status){
  var result;
  switch(status){
    case 0:
        result = layer.confirm('是否确认关闭', {
            btn: ['确认关闭', '再考虑一下'] //按钮
        }, function () {
            layer.msg('操作成功',{icon:1});
            xiugai(id, status);
        },function () {
            list_ajax.ajax.reload(null,false);
        });
        break;
    case 1:
        result = layer.confirm('是否确认开启', {
            btn: ['确认开启', '再考虑一下'] //按钮
        }, function () {
            layer.msg('操作成功',{icon:1});
            xiugai(id, status);
        },function () {
            list_ajax.ajax.reload(null,false);
        });
        break;
    case -1:
        result = layer.confirm('是否确认删除', {
            btn: ['确认删除', '再考虑一下'] //按钮
        }, function () {
            layer.msg('操作成功',{icon:1});
            xiugai(id, status);
        });
        break;
    default:
        result = layer.confirm('是否确认删除', {
            btn: ['确认删除', '再考虑一下'] //按钮
        }, function () {
            xiugai(id, status);
        });
        break;
  }

}
function xiugai(id, status) {
    var url = "{{ URL('admin/message/delete')}}";
    var data = {id:id,status:status};
    var result = puppet.myajax('post',url,data,false);
    if(result.code != 0){
       layer.msg(result.msg,{icon:2});
        return false;
    }else{
        list_ajax.ajax.reload(null,false);
    }
}
//查看消息详情
function messageInfo(kid) {
  $('#viewDetails').modal();
  $('#viewDetails h4').html("<span style='font-weight:bold;'></span> 消息详情");
  var data={kid:kid};
  var url = "{{URL('admin/message/massage/info')}}";
  var result = puppet.myajax('post',url,data,false);
  if(result.code == 1){
    layer.msg(result.msg,{icon:5});
    return false;
  }else{
    $('#viewDetails div.modal-body').html(result.msg);
  }

}
function searchData(){
  $("#data_list").dataTable().fnDraw(false);
  layer.msg('操作成功',{icon:6})
}
</script>
@endsection
